package cn.wolfcode.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.SystemDictionaryItemQueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model){
        //用于显示主分类
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();
        model.addAttribute("systemDictionaryList",systemDictionaries);

        //用于显示明细
        List<SystemDictionaryItem> parentitems = systemDictionaryItemService.selectParent();
        model.addAttribute("parentitems",parentitems);

        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.list(qo);
        model.addAttribute("result",pageInfo);

        return "systemDictionaryItem/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        if (systemDictionaryItem.getId()==null){
            systemDictionaryItemService.insert(systemDictionaryItem);
        }else{
            systemDictionaryItemService.updateByPrimaryKey(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        systemDictionaryItemService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }
}
